package com.dream.controller;



import com.alibaba.fastjson.JSON;
import com.dream.pojo.*;
import com.dream.service.*;
import com.dream.service.impl.*;
import com.dream.utils.PageUtil;
import com.dream.utils.ResultCommon;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

@WebServlet("/StudentController.do")
public class StudentController extends BaseServlet {

    StudentService studentService=new StudentServiceImpl();

    ClazzService clazzService=new ClazzServiceImpl();

    MajorService majorService=new MajorServiceImpl();

    DepartmentService departmentService=new DepartmentServiceImpl();

    DormitoryService dormitoryService=new DormitoryServiceImpl();

    TempRecordService tempRecordService=new TempRecordServiceImpl();

    VisitRecordService visitRecordService=new VisitRecordServiceImpl();

    UserService us = new UserServiceImpl();
    /**
     * 分页查询
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stuName = request.getParameter("stuName"); //条件1
        String pageIndex = request.getParameter("pageIndex"); //条件4  当前页码
        if (pageIndex == null || "".equals(pageIndex)) {
            pageIndex = "1";//默认值
        }
        String pageSize = request.getParameter("pageSize"); //条件5   页面大小
        if (pageSize == null || "".equals(pageSize)) {
            pageSize = "5";//默认值
        }
        //条数
        long totalCount = studentService.totalCount(stuName);
        //每页数据
        List<Student> studentList = studentService.list(stuName, Long.valueOf(pageIndex), Long.valueOf(pageSize));

        for (Student student : studentList) {
            String clazzId = student.getClazzId();
            Clazz clazz = clazzService.findById(clazzId);
            student.setClazzName(clazz.getClazzName());
            Major major = majorService.findById(clazz.getMajorId());
            student.setMajorName(major.getMajorName());
            Department department = departmentService.findById(major.getDeptId());
            student.setDeptName(department.getDeptName());
            String dormitoryId = student.getDormitoryId();
            Dormitory dormitory= dormitoryService.findById(dormitoryId);
            student.setDormitoryName(dormitory.getDormitoryName());
        }

        //封装一个分页的工具类
        PageUtil<Student> pageUtil = new PageUtil<Student>(Long.valueOf(pageIndex), Long.valueOf(pageSize), totalCount, studentList);
        //存值转发
        request.setAttribute("pageUtil", pageUtil);
        request.setAttribute("stuName", stuName); //回显
        request.getRequestDispatcher("viewStudentInfo.jsp").forward(request, response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultCommon resultCommon=null;
        String stuId = request.getParameter("stuId");
        String stuName = request.getParameter("stuName");
        String sex = request.getParameter("sex");
        String birthplace = request.getParameter("birthplace");
        String birthday = request.getParameter("birthday");
        String phoneNumber = request.getParameter("phoneNumber");
        String kinsfolkName = request.getParameter("kinsfolkName");
        String kinsfolkPhoneNumber = request.getParameter("kinsfolkPhoneNumber");
        String dormitoryId = request.getParameter("dormitoryId");
        String clazzId = request.getParameter("clazzId");
        String grade = request.getParameter("grade");
        String remark = request.getParameter("remark");
        Student student = new Student(stuId, stuName, sex, birthplace, Date.valueOf(birthday), phoneNumber, kinsfolkName, kinsfolkPhoneNumber, dormitoryId, clazzId, grade, remark, System.currentTimeMillis(), System.currentTimeMillis());
        int count = studentService.add(student);
        if (count > 0) {
            //新增user表，学生后期登录
            User user = new User(stuId, "123456", stuName, phoneNumber, "3", remark, System.currentTimeMillis(), System.currentTimeMillis());
            int count1 = us.add(user);
            if(count1>0){
                resultCommon=new ResultCommon(200,"新增成功",null);
            }else{
                resultCommon=new ResultCommon(500,"新增异常",null);
            }
        } else {
            resultCommon=new ResultCommon(500,"新增异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void goupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String stuId = request.getParameter("stuId");
        Student student = studentService.findById(stuId);

        String clazzId = student.getClazzId();
        Clazz clazz = clazzService.findById(clazzId);

        String majorId = clazz.getMajorId();
        Major major = majorService.findById(majorId);
        String deptId = major.getDeptId();

        String dormitoryId = student.getDormitoryId();

        request.setAttribute("deptId", deptId);
        request.setAttribute("majorId", majorId);
        request.setAttribute("clazzId", clazzId);
        request.setAttribute("dormitoryId", dormitoryId);

        request.setAttribute("student", student);
        request.getRequestDispatcher("modifyStuInfo.jsp").forward(request, response);
    }

    public void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        String stuId = request.getParameter("stuId");
        String stuName = request.getParameter("stuName");
        String sex = request.getParameter("sex");
        String birthplace = request.getParameter("birthplace");
        String birthday = request.getParameter("birthday");
        String phoneNumber = request.getParameter("phoneNumber");
        String kinsfolkName = request.getParameter("kinsfolkName");
        String kinsfolkPhoneNumber = request.getParameter("kinsfolkPhoneNumber");
        String dormitoryId = request.getParameter("dormitoryId");
        String clazzId = request.getParameter("clazzId");
        String grade = request.getParameter("grade");
        String remark = request.getParameter("remark");
        Student student = new Student(stuId,stuName,sex,birthplace, Date.valueOf(birthday), phoneNumber,kinsfolkName,kinsfolkPhoneNumber,dormitoryId,clazzId,grade,remark,System.currentTimeMillis(),System.currentTimeMillis());
        int count = studentService.update(student);
        if (count > 0) {
            resultCommon=new ResultCommon(200,"更新成功",null);
        } else {
            resultCommon=new ResultCommon(500,"更新异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stuId = request.getParameter("stuId");
        List<TempRecord> tempRecords = tempRecordService.findTempRecordsByStuId(stuId);
        List<VisitRecord> visitRecords = visitRecordService.findVisitRecordsByStuId(stuId);
        if(tempRecords.size()>0||visitRecords.size()>0){
            response.getWriter().write("<script>alert('学生数据在使用中，勿删除！');location.href='/StudentController.do?action=list';</script>");
        }else{
            int count = studentService.deleteById(stuId);
            if (count > 0) {
                response.getWriter().write("<script>alert('学生删除成功');location.href='/StudentController.do?action=list';</script>");
            } else {
                response.getWriter().write("<script>alert('学生删除异常');location.href='/StudentController.do?action=list';</script>");
            }
        }
    }
}
